﻿using AutoMapper;
using B.Rabbit.DataBase.Domain;
using B.Rabbit.DataBase.ErrorCode;
using B.Rabbit.DataBase.Infrastructure.Infrastructure;
using B.Rabbit.DataBase.Read.Api.Applocation.Command.Rabbit.Breeding;
using B.Rabbit.DataBase.Read.Api.DTOs;
using MediatR;

namespace B.Rabbit.DataBase.Read.Api.Applocation.Handler.Rabbit.Breeding
{
    public class GetBreedingCommandHandler : IRequestHandler<GetBreedingCommand, ApiResult<List<GetBreedingDTOs>>>
    {
        IBaseRepository<BreedingModel> breedingmodel;
        IMapper mapper;

        public GetBreedingCommandHandler(IBaseRepository<BreedingModel> breedingmodel, IMapper mapper)
        {
            this.breedingmodel = breedingmodel;
            this.mapper = mapper;
        }
        /// <summary>
        /// 显示养殖列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<GetBreedingDTOs>>> Handle(GetBreedingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<GetBreedingDTOs>> res = new ApiResult<List<GetBreedingDTOs>>();
            var breeding = breedingmodel.GetList().Where(x => x.IsDel == false);
            if (!string.IsNullOrEmpty(request.Batch))
            {
                breeding = breeding.Where(x => x.ProductionBatch == request.Batch);
            }

            var entity = mapper.Map<List<GetBreedingDTOs>>(breeding);
            
            res.data = entity;
            res.msg = "成功获取";
            res.code = StatusCode.Ok;
            return Task.FromResult(res);
        }
    }
}
